﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WanPiJie.Utilities;

namespace WanPiJie.WebSite.Management
{

    public partial class ProductPage : PageBase
    {

        /// <summary>
        /// 产品的属性值
        /// </summary>
		protected IDictionary<Int32, String> DicAttributes;

        /// <summary>
        /// 产品列表信息
        /// </summary>
        protected IList<Product> Products { get; set; }


        protected override void HttpGetProcess()
        {
            String catalogId = QV("cg");
            if (String.IsNullOrEmpty(catalogId))
                ShowMessage("参数错误");
            else
                this.Initialize(Int32.Parse(catalogId));
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="catalogId"></param>
        private void Initialize(Int32 catalogId)
        {
            var lisAttributes = ProductService.Instance.GetAttributeByCatalog(catalogId);
            this.DicAttributes = new Dictionary<Int32, String>();
            foreach (ProductAttribute attribute in lisAttributes)
                DicAttributes.Add(attribute.Id, attribute.Name);
            this.Products = ProductService.Instance.GetProductByCatalog(catalogId);
        }

        protected override void HttpPostProcess()
        {
            Int32 catalogId = Int32.Parse(PV("catalogId"));
            this.Initialize(catalogId);
            Product product = new Product
            {
                CatalogId = catalogId,
                 Name=PV("pname"),
                 Picture=String.Empty
            };
            product.Attributes = new Dictionary<int, string>();
            foreach (var key in this.DicAttributes.Keys)
            {
                product.Attributes.Add(key, PV(String.Format("attributes-{0}",key)));
            }

            ProductService.Instance.AddProduct(product);
        }
    }
}